package cn.sdhuijin.vms.websocket;

import java.io.IOException;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyMessageInboundPool {
	final static Logger logger = LoggerFactory
			.getLogger(MyMessageInboundPool.class);

	private static final Map<String, MyMessageInbound> map = new HashMap<String, MyMessageInbound>();

	public static void add(MyMessageInbound mi) {
		map.put(mi.getOrg(), mi);
		logger.info("添加map大小：" + map.size());
	}

	public static void remove(MyMessageInbound mi) {
		map.remove(mi.getOrg());
		logger.info("删除map大小：" + map.size());
	}

	@SuppressWarnings("deprecation")
	public static void sendMessage(String org, CharBuffer message) {
		MyMessageInbound mi = (MyMessageInbound) map.get(org);
		logger.info("发送信息内容：" + message);
		if (mi != null)
			try {
				mi.getWsOutbound().writeTextMessage(message);
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

	public static void broadcast(CharBuffer message) {
		Set<String> keySet = map.keySet();
		for (String key : keySet)
			sendMessage(key, message);
	}
}
